﻿using CSM_SQL.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using CSM_SQL;

namespace CommunitySuperMarket.Controllers
{
    public class LoginController : Controller
    {
        public CustomerBLL cb = new CustomerBLL();
        // GET: Login
        public ActionResult Login()
        {
            return View();
        }
        public ActionResult Register()
        {
            return View();
        }
        public ActionResult EditPerson()
        {
            var e = (Customer)HttpContext.Session["CurrUser"];
            if (e != null)
            {
                return View();
            }
            return RedirectToAction("Login", "Login");
        }

        public void GetNames()
        {
            HttpContext.Response.Write(JsonConvert.SerializeObject(cb.All().Select(c => c.Name)));
            HttpContext.Response.End();
        }

        public ActionResult Regist()
        {
            Customer c = new Customer
            {
                Name = Request.Form["name"],
                Password = MD5BLL.EncryptString(Request.Form["pwd"]),
                Address = Request.Form["address"],
                Phone = Request.Form["phone"]
            };
            CustomerBLL cb = new CustomerBLL();
            if (cb.Insert(c))
            {

                var CurrUser = new Customer
                {
                    C_ID = c.C_ID,
                    Address = c.Address,
                    Name = c.Name,
                    Phone = c.Phone
                };
                HttpContext.Session["CurrUser"] = CurrUser;

                return RedirectToAction("Home", "Home");
            }
                
            return RedirectToAction("Register");
                
        }

        public void Valid()
        {
            var name = Request.Params["name"];
            var pwd = Request.Params["pwd"];
            CustomerBLL cb = new CustomerBLL();
            var cus = cb.All().Where(a => (a.Name == name || a.Phone == name) && a.State != 1);
            if (cus.Count() > 0)
            {
                foreach(var c in cus)
                {
                    if(c.Password == MD5BLL.EncryptString(pwd))
                    {
                        var CurrUser = new Customer {
                            C_ID=c.C_ID,
                            Address=c.Address,
                            Name=c.Name,
                            Phone = c.Phone
                        };
                        Session["CurrUser"] = CurrUser;
                        var e = (Customer)HttpContext.Session["CurrUser"];
                        HttpContext.Response.Write(0);
                        HttpContext.Response.End();
                        return;
                    }
                }
            }
            HttpContext.Response.Write(1);
            HttpContext.Response.End();
        }

        public ActionResult Exit()
        {
            HttpContext.Session["CurrUser"] = null;
            return RedirectToAction("Login");
        }
    }
}